﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Xml;

namespace InkSpot
{
    /// <summary>
    /// Interaction logic for SymbolSVG.xaml
    /// </summary>
    public partial class SymbolSVG : SVG
    {
        #region Property:StrokeThickness
        /// <summary>
        /// Gets or sets the StrokeThickness assigned to the control.
        /// </summary>
        public double StrokeThickness
        {
            get { return (double)GetValue(StrokeThicknessProperty); }
            set
            {
                SetValue(StrokeThicknessProperty, value);
            }
        }

        /// <summary>
        /// Identifies the StrokeThickness dependency property.
        /// </summary>
        public static readonly DependencyProperty StrokeThicknessProperty =
            DependencyProperty.Register(
                "StrokeThickness", typeof(double), typeof(SymbolSVG),
                new FrameworkPropertyMetadata((double)1, new PropertyChangedCallback(OnStrokeThicknessChanged)));

        private static void OnStrokeThicknessChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            SymbolSVG control = (SymbolSVG)obj;

            RoutedPropertyChangedEventArgs<double> e = new RoutedPropertyChangedEventArgs<double>(
                (double)args.OldValue, (double)args.NewValue, StrokeThicknessChangedEvent);
            control.OnStrokeThicknessChanged(e);
        }

        /// <summary>
        /// Identifies the StrokeThicknessChanged routed event.
        /// </summary>
        public static readonly RoutedEvent StrokeThicknessChangedEvent = EventManager.RegisterRoutedEvent(
            "StrokeThicknessChanged", RoutingStrategy.Bubble,
            typeof(RoutedPropertyChangedEventHandler<double>), typeof(SymbolSVG));

        /// <summary>
        /// Occurs when the StrokeThickness property changes.
        /// </summary>
        public event RoutedPropertyChangedEventHandler<double> StrokeThicknessChanged
        {
            add { AddHandler(StrokeThicknessChangedEvent, value); }
            remove { RemoveHandler(StrokeThicknessChangedEvent, value); }
        }

        /// <summary>
        /// Raises the StrokeThicknessChanged event.
        /// </summary>
        /// <param name="args">Arguments associated with the StrokeThicknessChanged event.</param>
        protected virtual void OnStrokeThicknessChanged(RoutedPropertyChangedEventArgs<double> args)
        {
            RaiseEvent(args);
        }
        #endregion

        public SymbolSVG()
        {
            InitializeComponent();
        }

        protected override void  After_OnSourceChanged(List<Shape> shapes)
        {
            Binding BindsForeground = new Binding("Foreground") { Source = this };
            Binding BindsBorderBrush = new Binding("BorderBrush") { Source = this };
            Binding BindsStrokeThickness = new Binding("StrokeThickness") { Source = this };
            foreach (Shape g_shape in shapes)
            {
                if (g_shape.Fill == null) g_shape.SetBinding(Shape.FillProperty, BindsForeground);
                if (g_shape.Stroke == null)
                {
                    g_shape.SetBinding(Shape.StrokeProperty, BindsBorderBrush);
                    g_shape.SetBinding(Shape.StrokeThicknessProperty, BindsStrokeThickness);
                }
            }
            shapes.Clear();
        }
    }
}

